<?php
/*
* Author: Nguyen Hai Nam
* Module: Config_CategoryModule
* Desc	: Introduce pick.vn and sidle pic
* Date	: 12/12/2011
*/
class Static_PageModule extends Pick_Core_Module{
	
	public $authentication = 1;
	
	function Static_PageModule(){

		parent::Pick_Core_Module();
		$this->theme = "admin";
		# Languages
		$languages = new Pick_Core_Languages();
		$objectLanguages = "";
		$objectLanguages = $languages->getObjects(1, 'status = 1', array('position'=>'ASC'), '');
		if($objectLanguages){
			$objectLanguages = $objectLanguages['data'];
		}#endif
		$this->template->assign("objectLanguages", $objectLanguages);
        
        // Get Path for this module
        $this->template->assign('arrayParentPath', $this->getArrayParentPath());
	}
	public function getContent(){
		
		$act = $this->request->element('act', "list");
		$this->template->assign('act', $act);
		if($act == "position"){
			$statics 		= new Pick_Dao_Statics();
			$oId = $this->request->element('id','');
			$val = $this->request->element('val','');
			$arrData = array('position'=>$val);
			echo $statics->updateData($arrData, $oId);
			die;
		}
		if($act == "add"){
			$this->addAction();
		}else{
			$this->listAction();
		}

	}
	public function listAction(){
		
		$statics	 	= new Pick_Dao_Statics();
		$actions 		= new Pick_Core_Actions();
		$actions->setInfoAction($statics, $this->request);
		
		$infoText = '';
		$infoClass = 'infoText';
		$items_per_pages = DEFAULT_ADMIN_ROWS_PER_PAGE;
		
		# Read parameters from request
		$searchTerms 	= $this->request->element('searchTerms','');
		$searchStatus 	= $this->request->element('searchStatus','-1');
		$searchCategory = $this->request->element('searchCategory','-1');
		$page 			= $this->request->element('page',1);
		$orderBy		= $this->request->element('orderBy','position');
		$orderDir 		= $this->request->element('orderDir','DESC');
		
		# Control plus action
		$plus = $this->request->element('plus','');
		if($plus){
			$actions->auto($plus);
			$infoText 	= $actions->getInfoAction();
			$infoClass 	= "errorText";		
		}
		
		# Build search condition
		$searchCond = "1=1";
		$defaultName = $this->lang."_name";
		if($searchStatus != '-1') 	$searchCond .= " AND status = '$searchStatus'";
		if($searchTerms) 			$searchCond .= " AND ".$defaultName." LIKE '%".$searchTerms."%'";
		
		
		# Get all categories object and pass to template
		$listObjects = $statics->getObjects($page, $searchCond, array($orderBy => $orderDir), $items_per_pages);
		
		# Generate status combobox for Search form
		$listStatus = optionStatus($searchStatus);	
			
		# Pass some variables to template
		$this->template->assign('page', $page);
		$this->template->assign('listStatus', $listStatus);
		$this->template->assign('listObjects', $listObjects);
		$this->template->assign('searchTerms', $searchTerms);
		$this->template->assign('searchStatus', $searchStatus);
		$this->template->assign('orderBy', $orderBy);
		$this->template->assign('orderDir', $orderDir);
		$this->template->assign('infoText', $infoText);
		$this->template->assign('infoClass', $infoClass);

	}
	
	public function addAction(){

		$statics 		= new Pick_Dao_Statics();
		$objectInfo 	= new Pick_Dao_StaticsInfo();
		$textfilter 	= new Pick_Data_TextFilter();

		$searchTerms	 = $this->request->element('searchTerms','');
		$searchStatus	 = $this->request->element('searchStatus','-1');
		$orderBy		 = $this->request->element('orderBy','id');
		$orderDir		 = $this->request->element('orderDir','ASC');
		$searchStatus	 = $this->request->element('searchStatus','-1');
		$page 			 = $this->request->element('page',1);
		$plus 			 = $this->request->element('plus','');
		$oId 			 = $this->request->element('oId', 0);
		$infoText 		 = '';
		$infoClass 		 = "infoText";

		# Read parameters from request
		$arrayFieldFromMySQL = $statics->getListFields();
		$object = $statics->getObject($oId);
		if(is_object($object) && $this->request->post('plus')==""){
			foreach($arrayFieldFromMySQL as $fieldName){
				$fields[$fieldName] = $object->getField($fieldName);
			}#endforeach
		}else{
			foreach($arrayFieldFromMySQL as $fieldName){
				$fields[$fieldName] = $this->request->element($fieldName,'');
			}#endforeach
		}
		# Modify item of array
		$fields['slug'] = $textfilter->urlize($fields[$this->lang."_name"],false,'-');
		$fields['typeid'] = 4;// For only static page
        $background 	= isset($_FILES['background'])?$_FILES['background']:'';

		# Gan gia tri cho doi tuong
		$objectInfo->setArrayField($fields);
		$this->template->assign("objectInfo", $objectInfo);

		# Truy du lieu qua template
		$listStatus = optionStatus($objectInfo->getField('status'), DEFAULT_ADMIN_LANGUAGE);
		$this->template->assign("listStatus",$listStatus);

		foreach($arrayFieldFromMySQL as $fieldName){
			$this->template->assign($fieldName, $objectInfo->getField($fieldName));
		}#endforeach

		// Bat dau qua trinh su li du lieu
		if($_POST && $plus == 'saveItem') {

			# Validation
			$validate 	= new Pick_Data_Validate();
			$validatemess = $this->validateData($this->request, $validate);

			if($validatemess == '') {

				$object = $statics->getObjectFromSlug($objectInfo->getField('slug'));
				/* Kiem tra neu mau tin da duoc them thi khong cho them nua */
				$object = "";

				if($oId<=0){// Truong hop them thi moi kiem tra slug
					$object = $statics->getObjectFromSlug($objectInfo->getField('slug'));
				} 

				if(is_object($object)){
					$infoText = l('item_save_error');
					$infoClass = "InfoText";
				}else{
					/* Duong dan */
                    $backgroundbath = ROOT_PATH."/upload/images/background/";

					$isUpload = 0;
					$isUpload = $statics->isUploadFile($background['name']);
					if ($isUpload == 1){
						$avatarUpload = new Pick_Core_Upload($background);
						$copy = $avatarUpload->moveFile($backgroundbath);

						if(!$copy){
							$validatemess .= l('cannotuploadfile') . l('check_type');
						}
                        $nameAvatar = $avatarUpload->getNameFile();
                        $objectInfo->setField('background', $nameAvatar);
					}
					$ok = $statics->updateData($objectInfo->getArrayField(), $oId);
					$infoText = $validatemess;
					if($ok){
						$infoText = l('item_save_ok');
						$infoClass = "InfoText";
						$oId = $oId?$oId:mysql_insert_id();			
					}
				}

			}else {

				$infoText = l('error')."<br />".$validatemess;
				$infoClass = "errorText";

			}#endif

		}#endif

		$this->template->assign("oId",$oId);
		$this->template->assign("orderBy",$orderBy);
		$this->template->assign("orderDir",$orderDir);
		$this->template->assign("infoText",$infoText);
		$this->template->assign("infoClass",$infoClass);

	}

	function validateData($request, $validate) {
		$infoText = '';
		$infoText .= $validate->validString($this->l('name'), $request->element($this->lang."_name"));		
		return $infoText;
	}
}